package com.study.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author study
 * @version 1.0
 * @date 2020/8/8 22:07
 */
@Configuration
public class ThreadPoolConfig {
    //https://crossoverjie.top/JCSprout/#/thread/ThreadPoolExecutor

    /**
     * 消费队列线程
     *
     * @return
     */
    @Bean(value = "consumerQueueThreadPool")
    public ExecutorService buildConsumerQueueThreadPool() {

        ThreadFactory nameThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("consumer-queue-thread-%d").build();

        ExecutorService pool = new ThreadPoolExecutor(
                5,
                5,
                0L,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                nameThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return pool;
    }

    @Bean(value = "nvshensThreadPool")
    public ExecutorService buildnvshensThreadPool() {

        ThreadFactory nameThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("ns-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(
                6,
                8,
                0L,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                nameThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        return pool;
    }
}
